package com.hzdp.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzdp.dao.PatientDao;
import com.hzdp.dao.RecordDao;
import com.hzdp.entity.Patient;
import com.hzdp.entity.Record;
import com.hzdp.service.RecordService;

@Service
public class RecordServiceImpl implements RecordService {

	@Autowired
	private RecordDao recordDao;

	@Autowired
	private PatientDao patientDao;

	protected Logger logger = Logger.getLogger(getClass());

	@Override
	public List<Record> findAll() {
		List<Record> records = null;
		try {
			records = recordDao.findAll();
		} catch (SQLException e) {
			logger.error(e, e);
		}
		return records;
	}

	@Override
	public Record findById(int id) {
		Record record = null;
		try {
			record = recordDao.find(id);
		} catch (SQLException e) {
			logger.error(e, e);
		}
		return record;
	}

	@Override
	public void save(Record record) {
		try {
			Patient patient = patientDao.find(record.getPatientId());
			record.setPatientName(patient.getName());
			recordDao.save(record);
		} catch (SQLException e) {
			logger.error(e, e);
		}
	}

}
